<template>
    <div class="page homeMore-page" data-page="homeMore" style="overflow: hidden;">
        <div class="homeMore-top">
            <div class="left-block bradge-top-color" id="bind-change-unit_homeMore">
                <i class="iconfont icon-cebianlan" id="go-to-single"></i>
                <i class="iconfont icon-fanhui" id="go-to-home" style="font-size: 20px;margin-right: 5px;" ></i>
                <span id="bradge_org_name">总览</span>
            </div>
            <div class="right-block">
                <a href="#" class="link icon-only top-right-icon bind-homeScanning" style="padding-right:20px;">
                    <i class="iconfont icon-saoyisaonfc top-right-icon"></i>
                </a>
                <a href="/keyAlarm/" class="link icon-only top-right-icon "  data-view=".view-main" style="margin-top: -3px;">
                    <i class="iconfont icon-APPicon_yijianbaojing- key" style="font-size: 25px;"></i>
                </a>
            </div>
        </div>
        <div class="page-content home-content ptr-content" @ptr:refresh="refreshCards" id="page-content-scroll">
            <div class="ptr-preloader home-top-refresh">
                <div class="preloader color-multi"></div>
                <div class="ptr-arrow"></div>
            </div>
            <!--支队切换到单位头部-->
            <div class="card-safeIndex-zt-to-single">
                <div class="single-bottom-container">
                    <div class="bottom-num safeColor">
                        <a class="forward_score_detail safeColor">
                            <p id="change-home-unit-score"></p>
                            <span id="change-home-unit-type"></span>
                        <a href="/unitSafetyDetail/154/60/"></a>
                    </div>
                </div>
            </div>
            <!-- 单位首页头部 -->
            <div class="card-safeIndex-single-top">
                <div class="single-bottom-container">
                    <div class="bottom-num safeColor">
                        <a class="forward_score_detail safeColor">
                            <p id="home-unit-score"></p>
                            <span id="home-unit-type"></span>
                         </a>
                    </div>
                </div>
            </div>
            <!-- 首页卡片 -->
            <div class="cards-content unit-cards-content"></div>
            <!-- 首页页脚 -->
            <div class="bottom-custom-cards">
                <div class="set-custom-cards-btn bind-set-card">
                    <span class="iconfont icon-bianji1" style="font-size: 14px;margin-right: 2px;"></span>定制
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    return {
        mounted() {
            var self = this;
            common.loading(1);
            //判断是否切换了单位
            if(!isChangeUnitRole){
                self.getCards();
            }else{
                self.changeUnit(single_Orgid,single_OrgName);
            }
        },
        data: function () {
            return {
                cardsList: [],  //定义接收的字段
                userIsMuilUnitUser:userInfor.isMuilUnitUser,
                pageName:"home",
            }
        },
        methods: {
            //配置页面
            setPage:function(){
                var self = this;
                //判断当前是单联网还是多联网角色
                //没有切换角色
                $(".card-safeIndex-zt-to-single").removeClass('redIndex-wrap yellowIndex-wrap greenIndex-wrap');
                if(!isChangeUnitRole){
                    if(isChildPlatRole){//多平台切换到单平台
                        jQuery("#go-to-single,.card-safeIndex-zd-top").hide();
                        jQuery("#go-to-home,.card-safeIndex-zt-to-single").hide();
                        jQuery("#go-to-home").show();
                        jQuery(".bradge_org_name").css("float","left");
                        jQuery("#home-navbar").css("background-color","#333");
                        if(single_OrgName.length > 8){
                            jQuery("#bradge_org_name").html(single_OrgName.substr(0,8)+"...");
                        }else{
                            jQuery("#bradge_org_name").html(single_OrgName);
                        }
                        
                        jQuery(".bottom-custom-cards").hide(); //定制卡片
                        jQuery(".unit-cards-content").css("margin-top","0px");
                        jQuery(".card-safeIndex-zd").css("background-color","#102335");
                    }else{
                        if (self.userIsMuilUnitUser == 0) {
                            jQuery(".card-safeIndex-zd-top").hide();
                            jQuery(".card-safeIndex-single-top").show();
                            jQuery(".unit-cards-content").css("margin-top","-95px");
                            jQuery("#single_org_name").html(single_OrgName);
                            self.getSafeEvaluateListByOrgIdInfo();
                        }else {
                            isChildPlatRole=false;
                            jQuery("#go-to-single,.card-safeIndex-zd-top").show();
                            jQuery("#go-to-home,.card-safeIndex-zt-to-single").hide();
                            jQuery("#bradge_org_name").html("总览");
                            jQuery(".unit-cards-content").css("margin-top","-35px");
                            jQuery(".card-safeIndex-zd").css("background-color","#102335");
                        }                
                        single_Orgid = userInfor.organizationIds;
                        jQuery(".bottom-custom-cards").show();
                      }
                }else{
                    if(isChildPlatRole){//多平台切换到单平台
                        jQuery("#go-to-single,.card-safeIndex-zd-top").hide();
                        jQuery("#go-to-home,.card-safeIndex-zt-to-single").hide();
                        jQuery("#go-to-home").show();
                        jQuery(".bradge_org_name").css("float","left");
                        jQuery("#home-navbar").css("background-color","#333");
                        if(single_OrgName.length > 8){
                            jQuery("#bradge_org_name").html(single_OrgName.substr(0,8)+"...");
                        }else{
                            jQuery("#bradge_org_name").html(single_OrgName);
                        }
                        
                        jQuery(".bottom-custom-cards").hide(); //定制卡片
                        jQuery(".unit-cards-content").css("margin-top","0px");
                        jQuery(".card-safeIndex-zd").css("background-color","transparent");
                    }else{//多联网切换到单联网
                        jQuery("#go-to-single,.card-safeIndex-zd-top").hide();
                        jQuery("#go-to-home,.card-safeIndex-zt-to-single").show();
                        if(single_OrgName.length > 8){
                            jQuery("#bradge_org_name").html(single_OrgName.substr(0,8)+"...");
                        }else{
                            jQuery("#bradge_org_name").html(single_OrgName);
                        }
                        
                        jQuery(".bottom-custom-cards").hide(); //定制卡片
                        self.getSafeEvaluateListByOrgIdInfo(1);
                        jQuery(".unit-cards-content").css("margin-top","-95px");
                        jQuery(".card-safeIndex-zd").css("background-color","transparent");
                    }
                }
            },
            //联网单位安全评估
            getSafeEvaluateListByOrgIdInfo:function(isChangge){
                let $scoreEl = "";
                let $typeEl = "";
                let $typeBgEl = "";
                let $topNav = "";
                Dao.getSafeEvaluateListByOrgId({
                    userName: userInfor.accountName,
                    deviceId: appKeyObj.deviceId,
                    orgId:single_Orgid,
                    queryType:1,
                    pageIndex:0,
                    pageSize:1,
                },function (data) {
                    if(data){
                         var scorenumber =data[0].orgScore.toString();
                         if(scorenumber){
                            var scoreArr = scorenumber.split(".");
                            if(scoreArr.length==1){
                                scorenumber = scoreArr[0];
                            }else{
                                scorenumber = scoreArr[0] + "<span style='font-size: 30px;'>."+scoreArr[1]+"</span>";
                            }
                         }else{
                            scorenumber = 0;
                         }
                         var score = common.getScore(scorenumber);
                         jQuery('.forward_score_detail').attr('href',"/unitSafetyDetail/" + single_Orgid + "/" + data[0].orgScore + "/");
                         if(isChangge){
                             $typeBgEl = $(".card-safeIndex-zt-to-single");
                             $scoreEl = $("#change-home-unit-score");
                             $typeEl = $("#change-home-unit-type");
                         }else{
                             $typeBgEl = $(".card-safeIndex-single-top");
                             $scoreEl = $("#home-unit-score");
                             $typeEl = $("#home-unit-type");
                         }
                        jQuery("#home-navbar").removeClass('redIndex-wrap-top yellowIndex-wrap-top greenIndex-wrap-top');
                        $typeBgEl.removeClass('redIndex-wrap yellowIndex-wrap greenIndex-wrap');
                         if (score==10){
                             $typeBgEl.addClass('yellowIndex-wrap');
                             jQuery("#home-navbar").addClass("yellowIndex-wrap-top");
                             $scoreEl.empty().html(scorenumber);
                             $typeEl.empty().html('危险');
                        } else if(score==20){
                             $typeBgEl.addClass('redIndex-wrap');
                             jQuery("#home-navbar").addClass("redIndex-wrap-top");
                             $scoreEl.empty().html(scorenumber);
                             $typeEl.empty().html('告急');
                        }else{
                             $typeBgEl.addClass('greenIndex-wrap');
                             jQuery("#home-navbar").addClass("greenIndex-wrap-top");
                             $scoreEl.empty().html(scorenumber);
                             $typeEl.empty().html('安全');
                        }
                        if(scorenumber=="100"){
                            if(screen.width<=320){
                                jQuery(".bottom-num").css("margin-left","2px");
                            }else{
                                jQuery(".bottom-num").css("margin-left","15px");
                            }
                        }else{
                            jQuery(".bottom-num").css("margin-left","25px");
                        }
                    }
                },true);
            },
            //多联网切换至单联网
            changeUnit:function(orgId,orgName){
                var self = this;
                single_Orgid = orgId;
                single_OrgName = orgName;
                isChangeUnitRole = true;
                common.loading(1);
                self.userIsMuilUnitUser = 0;
                self.setPage();
                self.getCards();
            },
            //单联网返回至多联网
            singleBackToMore:function(){
                var self = this;
                common.loading(1);
                single_Orgid = userInfor.organizationIds;
                single_OrgName = "";
                isChangeUnitRole = false;
                isChildPlatRole=false;
                isFirstChangeUnit = true;
                self.userIsMuilUnitUser = userInfor.isMuilUnitUser;
                self.setPage();
                self.getCards();
            },
             //多平台去子平台
            morePlatToSingle:function(orgIds, platName){
                var self = this;
                single_Orgid = orgIds
                single_OrgName = platName;
                isChangeUnitRole = false;
                isChildPlatRole = true;
                common.loading(1);
                self.userIsMuilUnitUser = userInfor.isMuilUnitUser;
                self.setPage();
                self.getCards();
            },
            //获取卡片数据
            getCards: function(){
                var self = this;
                var $ = self.$$;
                var orgId="";
                if(single_Orgid.toString().indexOf(",")==-1){
                    orgId=single_Orgid;
                }else{
                    orgId="";
                }
                Dao.unitStateList({
                    userName: userInfor.accountName,
                    deviceId: appKeyObj.deviceId,
                    userAccount: userInfor.accountName,
                    isMuilUnitUser: self.userIsMuilUnitUser,
                    status: 1,
                    orgCode: platformCode || app.data.defaultPlatCode,
                    orgId:orgId
                }, function (data) {
                    self.cardsList = data;
                    globalCardList = data;
                    self.setCardDom(data);
                });
            },
            //设置卡片Dom
            setCardDom: function () {
                var self = this;
                var $ = self.$$;
                jQuery(".cards-content").empty();
                if (self.cardsList && self.cardsList.length > 0) {
                    for (var i = 0; i < self.cardsList.length; i++) {
                        $(".cards-content").append('<div class="view  card-view-style card-view-' + self.cardsList[i].id + '" id="' + self.cardsList[i].link + '" data-name="' + self.cardsList[i].link + '"></div>');
                    }
                    self.showEveryCards();
                }else{
                    $(".cards-content").append(`<div class="no-data-img" style="margin-top: 50%;"><img src='`+listNoDataPic()+`'/></div>`);
                }
                common.loading(0);
            },
            //卡片嵌入页面
            showEveryCards: function () {
                var self = this;
                var $ = self.$$;
                if (self.cardsList && self.cardsList.length > 0) {
                    for (var i = 0; i < self.cardsList.length; i++) {
                        app.views.create('#' + self.cardsList[i].link, {url: '/' + self.cardsList[i].link + '/'});
                    }
                }
            },
            //刷新
            refreshCards: function (e, done) {
                var self = this;
                var $ = self.$$;
                $(".home-top-refresh").show();
                if (done && e) {
                    setTimeout(function () {
                        self.getCards();
                        done();
                        $(".home-top-refresh").hide();
                    },1000);
                }
            },
        },
        on: {
            pageInit: function (e, page) {
                var self = this;

                //最后一个page进入的时候
                if(self.cardsList.length > 0){
                    if(self.cardsList[self.cardsList.length-1].link == page.route.name){
                        //实时安全指数
                        $("#realSafety .card").css("box-shadow","none");
                        self.setPage();
                        setTimeout(function(){
                            common.loading(0);
                        },500);
                    }
                }
                
                window.getCards_homeMore = self.refreshCards;

                //定制
                jQuery(".bind-set-card").click(function(event) {
                    app.router.navigate("/editCard/");
                });

                //原生扫一扫
                jQuery(".bind-homeScanning").off().click(function () {
                    native.scanQrCode();
                });

                //切换联网单位
                jQuery("#bind-change-unit_homeMore").off().click(function () {
                    if(isChildPlatRole){//多平台切换到单平台
                        app.view.get("#home").router.currentPageEl.f7Component.singleBackToMore();
                    }else{
                        if(!isChangeUnitRole){
                            //首页选择联网单位回调
                            loadSelectUnitFunc(function (data) {
                                app.view.get("#home").router.currentPageEl.f7Component.changeUnit(data.orgId,data.orgName);
                            });
                        }else{
                            app.view.get("#home").router.currentPageEl.f7Component.singleBackToMore();
                        }  
                    }
                });

                if(app.data.isFirstLoad){
                    app.data.isFirstLoad=false;
                    getPublicReouse();
                }
            },

        }
    }
</script>